'''The function to generate a random distribution'''

from scipy import *

def generate_theta(n=None):
    '''generate random number in 0 to pi range, with sin(theta) weighing.'''
    if n:
        pool = random.random(n)
    else:
        pool = random.random()
    out = arccos(1-2*pool)
    return out

def generate_phi(n=None):
    '''generate phi in 0 to pi, with sin(phi) weighing.
    '''
    if n:
        pool = random.random(n)
        out = arccos(1-2*pool)
    else:
        pool = random.random()
        out = arccos(1-2*pool)
    return out

def generate_phi_even(n=None):
    '''generate phi in 0 to 2pi, with sin(phi) weighing.
    In order to keep the numerical error in track, we make a hack, that the phi has equal weigh
    
    Note that the n has to be a odd number, in order this works.
    '''
    if n:
        m = int(n/2)
        pool = random.random(m)
        down = arccos(1-2*pool)
        up = down + pi
        out = concatenate((up, down))
    else:
        pool = random.random()
        if pool >=0.5:
            out = 2*pi-arccos(4*pool-3)
        else:
            out = arccos(1-4*pool)
    return out

def generate_lognorm_volume(n=None, mu=2.58041, sigma=0.121255):
    '''generate the volume distribution for a spherical particle with log-normal dist.
    Not the average and variance of the log-normal distribution is NOT the mu and sigma factor here.
    One should calculate the value again.
    '''
    #mu = 2.58041
    #sigma = 0.121255
    dia = random.lognormal(mu,sigma,n)*1e-9
    volume = pi*dia**3/6
    return volume


def generate_lognorm_volume_even(n=None, mu=2.58041, sigma=0.121255):
    '''generate the volume distribution for a spherical particle with log-normal dist.
    Not the average and variance of the log-normal distribution is NOT the mu and sigma factor here.
    One should calculate the value again.
    
    Use the same hack as the above function, in order to reduce numeric error.
    This should be used together with the even function for phi.
    '''
    #mu = 2.58041
    #sigma = 0.121255
    m = n/2
    dia = random.lognormal(mu,sigma,m)*1e-9
    volume = pi*dia**3/6
    volume = concatenate((volume, volume))
    return volume
